 <template>
 	<view>
 		<uni-forms :rules="rules" :value="formData" style=" border: 1px solid #000000;" ref="form"
 			validate-trigger="bind" err-show-type="undertext">
 			<uni-group>
 				<uni-forms-item name="scanCode" label="扫码">
 					<uni-easyinput type="text" :inputBorder="true" v-model="formData.scanCode" placeholder="..."
 						suffixIcon="scan" @iconClick="iconClick"></uni-easyinput>
 				</uni-forms-item>


 				<uni-forms-item name="checkmainmodu" v-if="false">
 					<uni-easyinput type="text" :inputBorder="true" v-model="formData.checkmainmodu"></uni-easyinput>
 				</uni-forms-item>
 				<!-- 使用原生input，需要绑定binddata -->
 				<uni-forms-item name="checkmain03" required label="分厂">

 					<uni-data-select @input="onInput" v-model="formData.checkmain03"  filterable  :localdata="checkmain03List"
 						v-if="ischeckmain03show" @blur="binddata('checkmain03', $event.detail.value)"
 						@change="change04"></uni-data-select>


 				</uni-forms-item>
 				<uni-forms-item name="checkmain05" required label="车间">

 					<uni-data-select v-model="formData.checkmain05"   filterable :localdata="checkmain05List"
 						v-if="ischeckmain05show" @blur="binddata('checkmain05', $event.detail.value)"
 						@change="change06"></uni-data-select>


 				</uni-forms-item>
 				<uni-forms-item required name="checkmain07"   filterable required label="模号/产线">


 					<uni-data-select v-model="formData.checkmain07" :localdata="checkmain07List"
 						v-if="ischeckmain07show" @blur="binddata('checkmain07', $event.detail.value)"
 						@change="change07"></uni-data-select>

 				</uni-forms-item>
 				<uni-forms-item name="checkmain10" label="机台号">
 					<uni-easyinput type="text" v-model="formData.checkmain10" placeholder="请输入机台号"></uni-easyinput>
 				</uni-forms-item>
 				<uni-forms-item name="checkmain11" required label="机台类型">
 					<uni-data-select v-model="formData.checkmain11"    filterable   :localdata="checkmain11List"
 						v-if="ischeckmain11show" @blur="binddata('checkmain11', $event.detail.value)"
 						@change="change10"></uni-data-select>
 				</uni-forms-item>
 				<uni-forms-item name="checkmain13" label="不良性质">
 					<uni-data-select v-model="formData.checkmain13"    filterable  :localdata="checkmain13List"
 						v-if="ischeckmain13show" @blur="binddata('checkmain13', $event.detail.value)"
 						@change="change13"></uni-data-select>
 				</uni-forms-item>
 				<uni-forms-item name="checkmain15" label="不良类型">
 					<uni-data-select v-model="formData.checkmain15"    filterable  :localdata="checkmain15List"
 						v-if="ischeckmain15show" @blur="binddata('checkmain15', $event.detail.value)"
 						@change="change15"></uni-data-select>
 				</uni-forms-item>
 				<uni-forms-item name="checkmain12" required label="检验类型">
 					<uni-data-select v-model="formData.checkmain12"   filterable  :localdata="checkmain12List"
 						v-if="ischeckmain12show" @blur="binddata('checkmain12', $event.detail.value)"
 						@change="checkmain12"></uni-data-select>
 				</uni-forms-item>
 			</uni-group>

 			<view>
 				<button class="short-button" type="primary" @click="submitForm('form')">确认选择</button>

 			</view>
 		</uni-forms>
 	</view>
 </template>

 <script>
 	import {
 		queryQheckmain04List,
 		queryQheckmain13List,
 		queryQheckmain15List,
 		queryDetailCheck,
 		getWxCofig
 	} from "@/api/gpc/gpc.js"
 	//自己封装的接口工具js
 	//	import api from '@/common/request/index'
 	//接口地址,环境等
 	import {
 		AGENT_ID
 	} from '@/config'
 	import jWeixin from 'weixin-js-sdk'




 	export default {
 		data() {

 			return {

 				name: this.$store.state.user.name,
 				version: getApp().globalData.config.appInfo.version,
 				ischeckmain03show: true,
 				checkmain03List: [],
 				ischeckmain05show: true,
 				checkmain05List: [],
 				ischeckmain07show: true,
 				checkmain07List: [],
 				ischeckmain11show: true,
 				checkmain11List: [],
 				ischeckmain13show: true,
 				checkmain13List: [],
 				ischeckmain15show: true,
 				checkmain15List: [],
 				ischeckmain12show: true,
 				checkmain12List: [{
 						value: 'HJ',
 						text: "检验类型1"
 					},
 					{
 						value: 'JH',
 						text: "检验类型2"
 					},
 					{
 						value: 'CH',
 						text: "检验类型3"
 					}, {
 						value: 'HC',
 						text: "检验类型4"
 					}, {
 						value: 'HH',
 						text: "检验类型5"
 					}, {
 						value: 'JJ',
 						text: "检验类型6"
 					}, {
 						value: 'CC',
 						text: "检验类型7"
 					}, {
 						value: 'MM',
 						text: "检验类型8"
 					}
 				],
 				formData: {

 					checkmainmodu: this.$store.state.user.name,
 					checkmain03: '',
 					checkmain05: '',
 					checkmain07: '',
 					checkmain10: '',
 					checkmain11: '',
 					checkmain12: 'HJ',
 					checkmain13: '',
 					checkmain15: '',
 					scanCode: ''

 				},

 				rules: {
 					checkmain03: {
 						rules: [{
 								required: true,
 								errorMessage: '请选择'
 							},
 							{
 								format: 'String',
 								errorMessage: '必须是字符串'
 							}
 						]
 					},
 					checkmain05: {
 						rules: [{
 							required: true,
 							errorMessage: '请选择'
 						}, {
 							format: 'String',
 							errorMessage: '必须是字符串'
 						}]
 					},
 					checkmain07: {
 						rules: [{
 								required: true,
 								errorMessage: '请选择'
 							},
 							{
 								format: 'String',
 								errorMessage: '必须是字符串'
 							}
 						]
 					},
 					checkmain10: {
 						rules: [{
 								required: false,
 								errorMessage: '请输入机台号'
 							},
 							{
 								format: 'checkmain10',
 								errorMessage: '请输入正确的机台号'
 							}
 						]
 					},
 					checkmain11: {
 						rules: [{
 								required: true,
 								errorMessage: '请选择'
 							},
 							{
 								format: 'String',
 								errorMessage: '必须是字符串'
 							}
 						]
 					},
 					checkmain13: {
 						rules: [{
 								required: false,
 								errorMessage: '请选择'
 							},
 							{
 								format: 'String',
 								errorMessage: '必须是字符串'
 							}
 						]
 					},
 					checkmain15: {
 						rules: [{
 								required: false,
 								errorMessage: '请选择'
 							},
 							{
 								format: 'String',
 								errorMessage: '必须是字符串'
 							}
 						]
 					},
 					checkmain12: {
 						rules: [{
 								required: true,
 								errorMessage: '请选择'
 							},
 							{
 								format: 'String',
 								errorMessage: '必须是字符串'
 							}
 						]
 					},
 					checkmainmodu: {
 						rules: [{
 								required: false,
 								errorMessage: '请选择'
 							},
 							{
 								format: 'String',
 								errorMessage: '必须是字符串'
 							}
 						]
 					}
 				}
 			}
 		},
 		async created() {
 			//this.queryQheckmain04List();
 			//this.getCofig();
 			//this.$modal.confirm(location.href.split('#')[0],'created')
 		},
 		onLoad: function(option) {


 			this.queryQheckmain04List();
 			if (navigator.userAgent.indexOf('iPhone') !== -1) {
 				window.wechaturl = window.location + '';
 			}
 			let local = encodeURIComponent(location.href.split('#')[0]);
 			this.getCofig(location.href.split('#')[0]);
 			debugger
 			let qywxscan = uni.getStorageSync('qywxscan');
 			if (!this.isEmpty(qywxscan)) {

 				this.setFormValue(JSON.parse(qywxscan).resultStr)
 			}
 			//this.setFormValue('Y/Z/24501/299/BL-BJ')
 		},
 		methods: {

 			isEmpty(value) {
 				return value === null || value === undefined || value === '' || (Array.isArray(value) && value.length ===
 					0) || (typeof value === 'object' && Object.keys(value).length === 0);
 			},
 			setFormValue(scancode) {
 				let arrtemp = scancode.split('/');
 				this.formData.scanCode = scancode
 				this.formData.checkmain03 = arrtemp[0]
 				this.scanchange(arrtemp[0])
 				this.formData.checkmain05 = arrtemp[1]
 				this.formData.checkmain07 = arrtemp[2]
 				this.formData.checkmain10 = arrtemp[3]
 				this.formData.checkmain11 = arrtemp[4]
 				this.formData.checkmain12 = 'HJ'
 				uni.removeStorageSync('qywxscan');

 			},

 			onInput(value) {
 				// 输入事件，根据输入的值来过滤选项
 				this.selectedValue = value;
 				this.filteredOptions = this.allOptions.filter(option =>
 					option.text.includes(value)
 				)
 			},


 			iconClick() {
 				const that = this;
 				let msg = ''
 				//#ifndef H5
 				uni.scanCode({
 					success: function(res) {
 						console.log("进来了1")
 						console.log('条码res：' + res);
 						console.log('条码类型：' + res.scanType);
 						console.log('条码内容：' + res.result);
 					},
 					fail: error => {
 						console.log("暂不支持1")
 					}
 				});
 				//#endif
 				//#ifdef H5
 				// this.log("暂不支持H5扫码 走onScan这个方法") 
 				jWeixin.scanQRCode({
 					needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
 					scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
 					success: function(res) {


 						//let local = location.href.split('#')[0]  +'&item=' + encodeURIComponent(JSON.stringify(res))

 						//let local = window.location.href.split("#")[0]; //获取当前页面地址作为回调地址
 						let scan = JSON.stringify(res)

 						uni.setStorage({
 							key: 'qywxscan',
 							data: scan,
 							success: function() {
 								/* 	uni.showToast({
 										title: "存储成功",
 										icon: "none",
 										duration: 2500
 									}) */
 								console.log('success');
 								location.reload()
 							}
 						});

 						//window.location.href =  local + "&item="+scan+"#/pages/index";
 						// var result = res.resultStr;
 						// window.location.href=result
 						/* uni.navigateTo({
 						  url: local
 						}) */

 					},
 					fail: function(response) {
 						uni.showToast({
 							title: "调用wx.scanQRCode扫码失败",
 							icon: "none",
 							duration: 2500
 						})
 						console.log("调用wx.scanQRCode扫码失败");
 					},
 				});
 				//#endif
 			},
 			async queryQheckmain04List() {

 				queryQheckmain04List('100').then((data) => {
 					// enterpriseTypelist.value = data.data;
 					let checkmain03Listtemp = []
 					//  debugger;
 					data.forEach((val, index) => {
 						checkmain03Listtemp.push({
 							value: val.mgGpbd01, // 选项id
 							text: val.mgGpbd02 // 选项名称
 						})
 					})
 					this.checkmain03List = checkmain03Listtemp
 					this.ischeckmain03show = true


 				});

 			},

 			async queryQheckmain06List(factory) {
 				queryQheckmain04List('101', factory).then((data) => {
 					let checkmain05Listtemp = []
 					data.forEach((val, index) => {
 						checkmain05Listtemp.push({
 							value: val.mgGpbd01, // 选项id
 							text: val.mgGpbd02 // 选项名称
 						})
 					})
 					this.checkmain05List = checkmain05Listtemp
 					this.ischeckmain05show = true

 				});

 			},
 			async queryQheckmain07List(factory) {
 				queryQheckmain04List('102', factory).then((data) => {
 					let checkmain07Listtemp = []
 					data.forEach((val, index) => {
 						checkmain07Listtemp.push({
 							value: val.mgGpbd01, // 选项id
 							text: val.mgGpbd02 // 选项名称
 						})
 					})
 					this.checkmain07List = checkmain07Listtemp
 					this.ischeckmain07show = true

 				});

 			},
 			async queryQheckmain11List(factory) {

 				queryQheckmain04List('200', factory).then((data) => {
 					let checkmain11Listtemp = []
 					data.forEach((val, index) => {
 						checkmain11Listtemp.push({
 							value: val.mgGpbd01, // 选项id
 							text: val.mgGpbd02 || val.mgGpbd01 // 选项名称
 						})
 					})
 					this.checkmain11List = checkmain11Listtemp
 					this.ischeckmain11show = true

 				});

 			},
 			async queryQheckmain13List(factory) {

 				queryQheckmain13List(factory).then((data) => {
 					let checkmain13Listtemp = []
 					data.forEach((val, index) => {

 						checkmain13Listtemp.push({
 							value: val.categorycode, // 选项id
 							text: val.categoryname || val.categorycode, // 选项名称,
 							fid: val.fid
 						})
 					})
 					this.checkmain13List = checkmain13Listtemp
 					this.ischeckmain13show = true

 				});

 			},
 			async queryQheckmain15List(fid) {

 				queryQheckmain15List(fid).then((data) => {
 					let checkmain15Listtemp = []
 					data.forEach((val, index) => {
 						checkmain15Listtemp.push({
 							value: val.subcode, // 选项id
 							text: val.subname || val.subcode // 选项名称
 						})
 					})
 					this.checkmain15List = checkmain15Listtemp
 					this.ischeckmain15show = true

 				});

 			},
 			scanchange(name) {
 				this.queryQheckmain06List(name);
 				this.queryQheckmain07List(name);
 				this.queryQheckmain11List(name);
 				this.queryQheckmain13List(name);
 			},
 			change04(name, value) {
 				//debugger;
 				this.queryQheckmain06List(name);
 				this.queryQheckmain07List(name);
 				this.queryQheckmain11List(name);
 				this.queryQheckmain13List(name);


 				//下拉框值设定，可自己设定
 				this.$refs.form.setValue(name, value)
 			},
 			change06(name, value) {},
 			change07(name, value) {},
 			change11(name, value) {},
 			change13(name, value) {

 				this.checkmain13List.forEach((val, index) => {
 					if (name === val.value) {
 						this.queryQheckmain15List(val.fid);
 					}
 				})

 			},
 			submitForm(form) {


 				this.$refs[form]
 					.submit()
 					.then(res => {
 						console.log('表单的值：', res)

 						debugger;
 						this.handleToEditInfo(this.formData);

 					})
 					.catch(errors => {
 						console.error('验证失败：', errors)
 					})
 			},

 			//重置表单 。原生的组件input组件不能重置表单
 			resetForm() {
 				this.$refs.form.resetFields()
 			},


 			clearValidate(form, name) {
 				if (!name) name = []
 				this.$refs[form].clearValidate(name)
 			},
 			handleToEditInfo(formData) {

 				queryDetailCheck(formData).then((data) => {
 					let that = this;


 					if (data.pageSize <= 0) {
 						this.$modal.msgError("无数据，请重新选择！")
 						return
 					}


 					if (data.hj) {
 						uni.showModal({
 							title: '提示',
 							content: '存在未完成单据：' + data.hj,
 							success: function(res) {
 								if (res.confirm) {
 									that.$tab.navigateTo('/pages/mine/gpc/detail?item=' +
 										encodeURIComponent(JSON.stringify(formData)))
 								} else if (res.cancel) {
 									that.$modal.msgError("已取消！")
 								}
 							}
 						});

 					} else {

 						this.$tab.navigateTo('/pages/mine/gpc/detail?item=' + encodeURIComponent(JSON.stringify(
 							formData)))
 					}
 				});




 			},



 			setValue(name, value) {
 				// 设置表单某项对应得值来触发表单校验
 				// 接受两个参数，第一个参数为表单域的 name ，第二个参数为表单域的值
 				this.$refs.form.setValue(name, value)
 			},

 			getCofig(url) {
 				//debugger
 				getWxCofig(url).then((res) => {
 					//	debugger
 					if (res) {
 						jWeixin.config({
 							beta: true,
 							debug: false, // 开启调试模式,
 							appId: res.appId, // 必填，企业号的唯一标识
 							timestamp: res.timestamp, // 必填，生成签名的时间戳
 							nonceStr: res.nonceStr, // 必填，生成签名的随机串
 							signature: res.signature, // 必填，签名
 							jsApiList: ['scanQRCode', 'checkJsApi', 'chooseImage'], // 必填，需使用的JS接口列表
 						});
 						jWeixin.ready(() => {
 							console.log('配置完成，扫码前准备完成')
 						});
 						jWeixin.error(function(res) {
 							location.reload()
 							//wx.config配置错误，会弹出窗口哪里错误，然后根据微信文档查询即可。
 							console.log('出错了：' + res.errMsg);
 						});
 					} else {
 						console.log('获取配置信息返回为空');
 					}

 				});



 			},









 		}



 	}
 </script>
 <style>
 	.uni-input-border {
 		padding: 0 10px;
 		height: 35px;
 		width: 100%;
 		font-size: 14px;
 		color: #666;
 		border: 1px #e5e5e5 solid;
 		border-radius: 5px;
 		box-sizing: border-box;
 	}

 	.short-button {
 		width: 100px;
 		/* 设置按钮的宽度 */
 		margin: 10px;
 		/* 按钮与其他元素的外边距 */
 		color: #fff;
 		padding: 10rpx 20rpx;
 		border-radius: 5rpx;
 		float: right;
 	}
 </style>